<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
              <a-form-item label="学号">
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="所属院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.ssyx" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="所属专业">
              <a-select allowClear v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.zyList" :key="index" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="开课院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.kkyx" @change="kkyxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程名称">
              <a-select allowClear v-model="queryParam.kcdm" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{i.kch}}-{{ i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
            <a-col :md="6" :sm="12">
                <a-form-item label="年级">
                    <a-select v-model="queryParam.sznj">
                        <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
                <a-form-item label="修课状态">
                    <a-select v-model="queryParam.xkzt">
                        <!-- <a-select-option v-for="i in TYPE0326" :key="i.id" :value="i.code">{{ i.name }}</a-select-option> -->
                        <a-select-option value="01">未修</a-select-option>
                        <a-select-option value="02">正在修课</a-select-option>
                        <a-select-option value="03">已经修课</a-select-option>
                        <a-select-option value="04">重修</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
                <a-button type="primary" @click="$refs.table.refresh(true)" style="margin-right:10px">查询</a-button>
                <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
                <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            </span>
            </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
          kcdm:'',
          sszy: ''
      },
      columns: [
        {
          title: '学号',
          dataIndex: 'xh'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '课程名',
          dataIndex: 'kcmc'
        },
        {
          title: '修课状态',
          dataIndex: 'xkztmc'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.scoreXkztList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList:[]
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0109: state => state.app['TYPE0109'],
      TYPE0358: state => state.app['TYPE0358'],
      TYPE0326: state => state.app['TYPE0326'],
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    // this.$store.dispatch('app/setDictionary', 'TYPE0326')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'ssyx', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.ssyx)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.queryParam.sszy = ''
      this.$set(this.selectMenu, 'zyList', [])
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    kkyxChange (code) {
      this.queryParam.kcdm = ''
      this.$set(this.selectMenu, 'courseList', [])
      if (code) {
        this.kcLoading = true
        this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    exportExcel () {
      let params = this.queryParam
      delete params.pageNum
      delete params.pageSize
      this.$export('/foster/kccj/xkztExp', {
        ...params
      })
    }
  }
}
</script>

<style>

</style>
